package com.puzzlebazar.client.util;

import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;

public class NumberFilter implements KeyPressHandler {

	public void onKeyPress(Widget sender, char keyCode, int modifiers) {

	}

	@Override
	public void onKeyPress(KeyPressEvent event) {
		assert( event.getSource() instanceof TextBox );
		TextBox sender = (TextBox)event.getSource();
		char keyCode = event.getCharCode();
		
		if ((!Character.isDigit(keyCode)) && (keyCode != (char) KeyCodes.KEY_TAB)
				&& (keyCode != (char) KeyCodes.KEY_BACKSPACE)
				&& (keyCode != (char) KeyCodes.KEY_DELETE) && (keyCode != (char) KeyCodes.KEY_ENTER) 
				&& (keyCode != (char) KeyCodes.KEY_HOME) && (keyCode != (char) KeyCodes.KEY_END)
				&& (keyCode != (char) KeyCodes.KEY_LEFT) && (keyCode != (char) KeyCodes.KEY_UP)
				&& (keyCode != (char) KeyCodes.KEY_RIGHT) && (keyCode != (char) KeyCodes.KEY_DOWN)) {
			// TextBox.cancelKey() suppresses the current keyboard event.
			sender.cancelKey();
		}
	}


}
